package com.streamsets.datacollector.pileline.stage;

import com.streamsets.datacollector.model.etl.FieldValueHashConfigJson;
import com.streamsets.datacollector.model.etl.bean.FieldValueHashBean;
import com.streamsets.datacollector.restapi.bean.ConfigConfigurationJson;
import com.streamsets.datacollector.restapi.bean.StageConfigurationJson;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  初始化 哈希及脱敏  的Stage的配置
 *
 * @author wengzhonghui
 * @date 2020/9/16 10:32
 */
public class InitFieldValueHashConfig {

    /*
     * stage instanceName名称
     */
    private static final String STAGE_NAME = "g2_01";

    /*
     * 名称
     */
    private static final String CONFIG_NAME_MASK = "hasherConfig.inPlaceFieldHasherConfigs";


    public static boolean init(StageConfigurationJson stage, FieldValueHashConfigJson configDto){
        if(STAGE_NAME.equalsIgnoreCase(stage.getInstanceName())){
            if (configDto != null) {
                List<ConfigConfigurationJson> configs = intConfig(stage.getConfiguration(), configDto);
                stage.setConfiguration(configs);
            }
        }
        return true;
    }

    public static List<ConfigConfigurationJson> intConfig(List<ConfigConfigurationJson> oldConfigs, FieldValueHashConfigJson configDto){
        List<ConfigConfigurationJson> newConfigs = new ArrayList<>();
        for(ConfigConfigurationJson config :oldConfigs ){
            config = initFieldValueHashConfigs(config, configDto);
            newConfigs.add(config);
        }
        return newConfigs;
    }


    /*
    * 把 列值屏蔽
    * 格式示例如：
    *[ {
          "sourceFieldsToHash" : [ "/sort_no" ],
          "hashType" : "MD5"
        }, {
          "sourceFieldsToHash" : [ "/id", "/subject_name" ],
          "hashType" : "MASK",
          "maskType" : "retain_before_2_after_4"
        }, {
          "sourceFieldsToHash" : [ "/created_by" ],
          "hashType" : "SHA1"
        }, {
          "sourceFieldsToHash" : [ "/subject_short_name" ],
          "hashType" : "SHA256"
        } ]
    * @return 返回新生成的配置
    */
    private static ConfigConfigurationJson initFieldValueHashConfigs(ConfigConfigurationJson config, FieldValueHashConfigJson configDto){
        if(CONFIG_NAME_MASK.equalsIgnoreCase(config.getName())){
            List configValList = new ArrayList();
            if(configDto != null && configDto.getFieldHashColumns() != null && configDto.getFieldHashColumns().size()>0){
							for(FieldValueHashBean valConfig : configDto.getFieldHashColumns()){
								if(valConfig == null ||  valConfig.getFields() == null || valConfig.getFields().size() == 0){
									continue;
								}
								Map data = new HashMap();
								data.put("sourceFieldsToHash", getFieldsArray(valConfig.getFields()));
								data.put("hashType", valConfig.getHashType());
								if(StringUtils.isNotEmpty(valConfig.getMaskType())){
									data.put("maskType", valConfig.getMaskType());
								}
								configValList.add(data);
							}
            }
            config = new ConfigConfigurationJson(CONFIG_NAME_MASK,configValList);
        }
        return config;
    }

	private static String[] getFieldsArray(List<String> fields){
		List<String> resultList = new ArrayList<>();
		for(String field : fields){
			resultList.add("/" + field);
		}
		String[] strArray = new String[resultList.size()];
		return resultList.toArray(strArray);
	}

}
